import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:my_flutter_core/config/general_config.dart';
import 'package:my_flutter_core/constants/date_patterns.dart';
import 'package:my_flutter_core/extensions/string_extension.dart';
import 'package:my_flutter_core/utils/date_time_utils.dart';

extension DateTimeFormatting on DateTime? {
  /// Formats the [date] follows the [dateFormat].
  /// If the [date] is null the empty string is return.
  String formatDate(String dateFormat) {
    return this != null ? DateFormat(dateFormat).format(this!) : '';
  }

  /// Formats the [date] follows the default date format.
  /// It returns UTC or local date format string based on [GeneralConfig.enableUtcFormat].
  /// If the [date] is null the empty string is return.
  String formatLongDateTime({bool? dateOnly}) {
    if (this == null) {
      return '';
    }

    var date = this!;

    const dateFormat = GeneralConfig.enableUtcFormat
        ? DatePatterns.utcLongDateTime
        : DatePatterns.longDateTime;

    if (dateOnly == true) {
      date = DateUtils.dateOnly(date);
    }

    if (GeneralConfig.enableUtcFormat) {
      date = date.toUtc();
    }

    return DateFormat(dateFormat).format(date);
  }
}

extension DateTimeManipulation on DateTime {}

extension DateTimeFriendlyText on DateTime {
  /// Converts the [date] to the text by [dateFormat] which contains day, week info.
  /// Example:
  /// - 2020-02-21 Friday Today.
  /// - 2020-02-20 Thursday Yesterday.
  /// - 2020-20-18 Tuesday.
  String convertDateToFriendlyText(String dateFormat) {
    // Prepares the text with the formatted date and week day.
    var text = '${formatDate(dateFormat)} ${DatePatterns.weekDay[weekday]}';

    var suffix = '';

    // Gets system current date.
    final today = getCurrentDateTime();

    // Checks if the input date is today, day after tomorrow, tomorrow, yesterday or the day before yesterday.
    // If so the extra day info would be applied.
    if (today.year == year && today.month == month && today.day == day) {
      suffix = ' 今天';
    }

    if (suffix.isNullOrEmpty) {
      final dayAfterTomorrow = today.add(const Duration(days: 2));

      if (dayAfterTomorrow.year == year &&
          dayAfterTomorrow.month == month &&
          dayAfterTomorrow.day == day) {
        suffix = ' 后天';
      }
    }

    if (suffix.isNullOrEmpty) {
      final tomorrow = today.add(const Duration(days: 1));

      if (tomorrow.year == year &&
          tomorrow.month == month &&
          tomorrow.day == day) {
        suffix = ' 明天';
      }
    }

    if (suffix.isNullOrEmpty) {
      final yesterday = today.add(const Duration(days: -1));

      if (yesterday.year == year &&
          yesterday.month == month &&
          yesterday.day == day) {
        suffix = ' 昨天';
      }
    }

    if (suffix.isNullOrEmpty) {
      final dayBeforeYesterday = today.add(const Duration(days: -2));

      if (dayBeforeYesterday.year == year &&
          dayBeforeYesterday.month == month &&
          dayBeforeYesterday.day == day) {
        suffix = ' 前天';
      }
    }

    return text + suffix;
  }
}
